﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Services;
using DailyDiet.Services.Navigation;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Views;
using Xamarin.Forms;

namespace DailyDiet.ViewModels
{
    /// <summary>
    /// 我的主页ViewModel
    /// </summary>
    public class MyPageViewModel : ViewModelBase {

        /******** 构造函数 ********/

        /// <summary>
        /// 内容页导航
        /// </summary>
        private IContentNavigationService _navigationService;

        private IPreferenceStorage _preferenceStorage;

        public MyPageViewModel(IContentNavigationService navigationService, IPreferenceStorage preferenceStorage)
        {
            _navigationService = navigationService;
            _preferenceStorage = preferenceStorage;
            EventItems = new List<string>();
        }


        /******** 绑定属性 ********/

        /// <summary>
        /// 用户昵称
        /// </summary>
        public string NickName {

            get => _nickName;
            set => Set(nameof(NickName), ref _nickName, value);
        }

        private string _nickName;

        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email {

            get => _email;
            set => Set(nameof(Email), ref _email, value);
        }

        private string _email;

        public List<string> EventItems
        {

            get => _eventItems;
            set => Set(nameof(EventItems), ref _eventItems, value);
        }

        private List<string> _eventItems;


        //私有变量
        private Shell _shell;
        public Shell Shell =>
            _shell ?? (_shell = Shell.Current);


        /******** 绑定命令 ********/


        /// <summary>
        /// 页面显示命令---显示昵称和邮箱
        /// </summary>
        private RelayCommand _pageAppearingCommand;
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand = new RelayCommand(async () => await PageAppearingCommandFunc()));

        public async Task PageAppearingCommandFunc() {

            NickName = _preferenceStorage.Get(PersonalInfoConstants.NickNameConstant, "LUOSHENLIU");
            Email = _preferenceStorage.Get(PersonalInfoConstants.EmailConstant,"11111111@163.com");
            EventItems = new List<string>(){"菜品大全","菜品收藏","版本信息"};
        }


        /// <summary>
        /// 跳转至个人信息设置界面命令
        /// </summary>
        private RelayCommand _personalInfoSetCommand;
        public RelayCommand PersonalInfoSetCommand =>
            _personalInfoSetCommand ?? (_personalInfoSetCommand = new RelayCommand(async () => await PersonalInfoSetCommandFunc()));

        public async Task PersonalInfoSetCommandFunc() {
            await _navigationService.NavigateToAsync(ContentNavigationServiceConstants
                .PersonalInfoSetPage);

        }

        /// <summary>
        /// 保存个人信息命令
        /// </summary>
        private RelayCommand _savePersonalInfoCommand;
        public RelayCommand SavePersonalInfoCommand =>
            _savePersonalInfoCommand ?? (_savePersonalInfoCommand = new RelayCommand(async () => await SavePersonalInfoCommandFunc()));

        public async Task SavePersonalInfoCommandFunc()
        {
            _preferenceStorage.Set(PersonalInfoConstants.NickNameConstant, NickName);
            _preferenceStorage.Set(PersonalInfoConstants.EmailConstant, Email);
            await Shell?.CurrentPage?.Navigation?.PopAsync();
        }



        private RelayCommand<string> _dealCommand;
        public RelayCommand<string> DealCommand =>
            _dealCommand ?? (_dealCommand =
                new RelayCommand<string>(async diets => await DealCommandFunc(diets)));

        public async Task DealCommandFunc(string key)
        {
            if (key.Equals("菜品大全")) {
                await _navigationService.NavigateToAsync(
                    ContentNavigationServiceConstants.AllDietsPage);
            } else if (key.Equals("菜品收藏")) {

                await _navigationService.NavigateToAsync(
                    ContentNavigationServiceConstants.FavoritePage);

            }
            else if (key.Equals("口味选择")) {

                await _navigationService.NavigateToAsync(ContentNavigationServiceConstants
                    .TasteSetPage);

            }
            else if (key.Equals("版本信息")) {
                await _navigationService.NavigateToAsync(
                    ContentNavigationServiceConstants.VersionPage);
            }
        }
    }

    public static class PersonalInfoConstants {

        public const string EmailConstant = "Email";
        public const string NickNameConstant = "NickName";

    }
}
